package com.lagou.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class TestService {
    @Reference
    public HelloService helloService;
    @Reference
    public EchoSerivce echoSerivce;
    @Reference
    private TPService tpService;

    private volatile boolean isExecute = true;
    private ThreadPoolExecutor threadPool = new ThreadPoolExecutor(100, 200, 100, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

    public String test() {
        echoSerivce.echo("do echo");
        return helloService.sayHello("do hello", 0);
    }

    public void executeMultiThread() {
        isExecute = true;
        while(isExecute) {
            for(int i = 0; i < 20; i++) {
                threadPool.execute(()-> tpService.methodA());
                threadPool.execute(()-> tpService.methodB());
                threadPool.execute(()-> tpService.methodC());
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopMultiThread() {
        isExecute = false;
    }
}
